﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Word = Microsoft.Office.Interop.Word;

namespace WordTemplate2.Pannel
{



    public partial class AddRefrence : Form
    {
        public struct TextData
        {
            public string data;
            public string head;
            public string version;
        }

        static List<string> s_list_array;
        public AddRefrence()
        {
            InitializeComponent();
            s_list_array = new List<string>();
            //第一级:GB,GB/T
            //第二级:GJB,GJB/Z
            //第三级:QJ,QJ/Z,HB,SJ,YB
            //第四级:Q/QJA
            //第五级:Q/RJ
            s_list_array.Add("GB");
            s_list_array.Add("GB/T");
            s_list_array.Add("GJB");
            s_list_array.Add("GJB/Z");
            s_list_array.Add("QJ");
            s_list_array.Add("QJ/Z");
            s_list_array.Add("HB");
            s_list_array.Add("SJ");
            s_list_array.Add("YB");
            s_list_array.Add("Q/QJA");
            s_list_array.Add("Q/RJ");

            foreach (string s in ThisDocument.exist_refrence_list)
            {
                listBox1.Items.Add(s);
            }

            
        }
        

        private void button1_Click(object sender, EventArgs e)
        {
            object move = Word.WdMovementType.wdMove;
            object extend = Word.WdMovementType.wdExtend;
            object character = Word.WdUnits.wdCharacter;
            object number = 1;
            object gotobookmark = Word.WdGoToItem.wdGoToBookmark;
            object name = "mbookmark4_ref";
            object varMissing = System.Type.Missing;

            Globals.ThisDocument.Application.ActiveWindow.View.SeekView = Word.WdSeekView.wdSeekMainDocument;
            Globals.ThisDocument.Application.Selection.GoTo(ref gotobookmark, ref varMissing, ref varMissing, ref name);
            Globals.ThisDocument.Application.Selection.TypeText(" ");
            Globals.ThisDocument.Application.Selection.TypeParagraph();
            for (int i = 0; i < listBox1.Items.Count; i++)
            {
                Globals.ThisDocument.Application.Selection.TypeText(listBox1.Items[i].ToString());
                Globals.ThisDocument.Application.Selection.TypeParagraph();
            }   

            this.Visible = false;
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Visible = false;
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            for (int i = listBox1.Items.Count - 1; i >= 0; i--)
            {
                if (listBox1.SelectedIndices.Contains(i))
                {
                    listBox1.Items.RemoveAt(i);
                }
            }   
   
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Length == 0)
            {
                MessageBox.Show("第一项不能为空");
                return;
            }
            else if (textBox2.Text.Length == 0)
            {
                MessageBox.Show("第二项不能为空");
                return;
            }

            string strData = textBox1.Text + " " + textBox2.Text;

            if(textBox3.Text.Length != 0)
            {
                strData += "-";
                strData += textBox3.Text;
            }

            if (textBox4.Text.Length != 0)
            {
                strData += "         ";
                strData += textBox4.Text;
            }
            listBox1.Items.Add(strData);

            UpdateStringInList();
        }

      
        public static int CompareTextData(TextData a, TextData b)
        {
            if (s_list_array.Contains(a.head) || s_list_array.Contains(b.head))
            {
                if (a.head != b.head)
                {
                    if ((a.head == "GB" && b.head == "GB/T") || ((a.head == "GB/T" && b.head == "GB")))
                    {
                        bool flag1 = Globals.ThisDocument.IsNumeric(a.version);
                        bool flag2 = Globals.ThisDocument.IsNumeric(b.version);
                        if(flag1 && flag2)
                        {
                            float aNum = float.Parse(a.version);
                            float bNum = float.Parse(b.version);
                            if (aNum == bNum)
                            {
                                return 0;
                            }
                            return aNum > bNum ? 1 : -1;
                        }
                        else
                        {
                            return 1;
                        }
                    }
                    foreach (string s in s_list_array)
                    {
                        if (a.head == s)
                        {
                            return -1;
                        }
                        else if (b.head == s)
                        {
                            return 1;
                        }
                    }

                }
                else
                {
                    bool flag1 = Globals.ThisDocument.IsNumeric(a.version);
                    bool flag2 = Globals.ThisDocument.IsNumeric(b.version);
                    if (flag1 && flag2)
                    {
                        float aNum = float.Parse(a.version);
                        float bNum = float.Parse(b.version);
                        if (aNum == bNum)
                        {
                            return 0;
                        }
                        return aNum > bNum ? 1 : -1;
                    }
                    else
                    {
                        return 1;
                    }
                }
            }
          

            return 0;
        }

        private void UpdateStringInList()
        {
            List<string> list = new List<string>();
            foreach (string str in listBox1.Items)
            {
                list.Add(str);
            }


            List<TextData> text_data_list = new List<TextData>();
            foreach (string str in list)
            {
                TextData data = new TextData();
                data.data = str;
                int count = 0;
                int head_mark = 0;
                foreach (char a in str)
                {
                    if (head_mark == 0)
                    {
                        if (a.ToString() == " ")
                        {
                            data.head = str.Substring(0, count);
                            head_mark = count + 1;
                        }
                    }
                    else
                    {
                        if (a.ToString() == " " || a.ToString() == "-")
                        {
                            data.version = str.Substring(head_mark, count - head_mark);
                            text_data_list.Add(data);
                            break;
                        }
                    }
                    count++;
                }
            }

            try
            {
                text_data_list.Sort(CompareTextData);
            }
            catch
            {
                MessageBox.Show("排序失败，请确认内容是否有异常");
            }
            
            listBox1.Items.Clear();
            foreach (TextData t in text_data_list)
            {
                listBox1.Items.Add(t.data);
            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            int l = textBox2.Text.Length;
            if (l != 0)
            {
                char t = textBox2.Text[l - 1];
                if (t == ' ' || t == '-')
                {
                    textBox2.Text = textBox2.Text.Remove(l - 1);
                    textBox2.SelectionStart = textBox2.Text.Length;
                    textBox2.SelectionLength = 0;
                }
            }
        }
    }
}
